<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
      xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link rel="stylesheet" href="/layui/css/layui.css">
  <link rel="stylesheet" href="/css/custom.form.css">
</head>
<body>
<div class="panel panel-default operation" hidden>
  <div class="panel-heading title"></div>
<div class="layui-card-body">
</div>
</div>

<div class="table_div">
  <div id="searchParam"  shiro:hasPermission="sysJob:list">
    <div class="layui-form-item">
      <div class="layui-input-inline">
        <input type="text" id="jobId" class="layui-input"  autocomplete="off" placeholder="请输入任务id">
      </div>

      <div class="layui-input-inline ">
        <button class="layui-btn"   id="search">查询</button>
        <button class="layui-btn"   id="export">导出全部</button>
      </div>
    </div>

  </div>
  <table class="layui-table" id="showTable" lay-filter="showTable" ></table>
  <div id="laypage"></div>
</div>
<script type="text/html" id="toolbar">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm" lay-event="batchDeleted" shiro:hasPermission="sysJob:delete">清空</button>
  </div>
</script>
</body>
</html>
<script src="/layui/layui.all.js"></script>
<script src="/js/core.util.js"></script>
<script>

  var table = layui.table;
  var laypage = layui.laypage
  var form = layui.form;
  var layer = layui.layer;
  var $ = jQuery = layui.jquery;
  var laydate = layui.laydate;

  layui.use(['table', 'laypage', 'layer', 'laydate'], function () {
    var ins1;
    var searchParam = {
      jobId: null,
      page: 1,
      limit: 10
    };
    //初始化 获取数据
    loadData();

    function loadData(page){
      searchParam.page = CoreUtil.isEmpty(page) ? searchParam.page : page;
      CoreUtil.sendAjax("/sysJobLog/listByPage", JSON.stringify(searchParam), function (res) {
        //初始化渲染数据
        if (res.data.records != null) {
          loadTable(res.data.records);
        }
        laypageTable(res.data.total);
      }, "POST", false, function (res) {
        layer.msg("抱歉！您暂无获取列表的权限");
        var noAuthorityData = [];
        loadTable(noAuthorityData);
      });
    }

    //渲染分页插件
    function laypageTable(total) {
      laypage.render({
        elem: 'laypage'
        , count: total
        , limit: searchParam.limit
        , layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
        , curr: searchParam.page //获取起始页
        , jump: function (obj, first) {
          if(!first){
            searchParam.page = obj.curr;
            searchParam.limit = obj.limit;
            loadData()
          }
        }
      });
    };

    //渲染table
    var loadTable = function (data) {
      //展示已知数据
      ins1 = table.render({
        elem: '#showTable'
        , cols: [
          [
            {type: 'checkbox', fixed: 'left'},
            {field: 'id', title: '任务日志id', sort: true},
            {field: 'jobId', title: '任务id', sort: true},
            {field: 'beanName', title: 'spring bean名称', sort: true},
            {field: 'params', title: '参数', sort: true},
            {field: 'status', title: '任务状态', width:110, sort: true, templet: function (item) {
                if (item.status === 1) {
                  return '  <button type="button"  class="layui-btn  layui-btn-xs layui-btn-danger">失败</button>';
                }
                if (item.status === 0) {
                  return '  <button type="button"  class="layui-btn   layui-btn-xs">成功</button>';
                }
              }},
            {field: 'error', title: '失败信息', sort: true},
            {field: 'times', title: '耗时(单位：毫秒)', sort: true},
            {field: 'createTime', title: '创建时间', sort: true}]
        ]
        , data: data
        , even: true
        , limit: data.length
        , limits: [10, 20, 30, 40, 50]
        , toolbar: '#toolbar'
      });
    };



    //执行查询
    $("#search").click(function () {
      searchParam.jobId = $("#jobId").val();
      loadData(1);
    });

    //导出
    $('#export').on('click', function () {
      //原先分页limit
      var limitOrg = searchParam.limit;
      searchParam.limit = 1000000;
      searchParam.jobId = $("#jobId").val();
      CoreUtil.sendAjax("/sysJobLog/listByPage", JSON.stringify(searchParam), function (res) {
        //初始化渲染数据
        if (res.data.records != null) {
          table.exportFile(ins1.config.id, res.data.records, 'xls');
        }
      }, "POST", false, function (res) {
        layer.msg("抱歉！您暂无获取列表的权限");
        var noAuthorityData = [];
        loadTable(noAuthorityData);
      });
      searchParam.limit = limitOrg;

    });

    //表头工具
    table.on('toolbar(showTable)', function(obj){
      switch(obj.event){
        case 'batchDeleted':
          layer.open({
            content: "确定要清空么?",
            yes: function(index, layero){
              layer.close(index); //如果设定了yes回调，需进行手工关闭
              CoreUtil.sendAjax("/sysJobLog/delete",{},function (res) {
                layer.msg(res.msg, {time:1000},function () {
                  loadData(1);
                });
              },"DELETE",false,function (res) {
                layer.msg("抱歉！您暂无权限");
              });
            }
          });

          break;
      };
    });

    //返回
    $("#btn_cancel").click(function() {
      $(".table_div").show();
      $(".operation").hide();
      return false;
    });

    //监听保存
    form.on('submit(submit)', function(data){
      if(data.field.id===undefined || data.field.id===null || data.field.id===""){
        CoreUtil.sendAjax("/sysJobLog/add",JSON.stringify(data.field),function (res) {
          $(".table_div").show();
          $(".operation").hide();
          loadData(1);
        },"POST",false,function (res) {
          layer.msg("抱歉！您暂无权限");
        });
      }else {
        CoreUtil.sendAjax("/sysJobLog/update",JSON.stringify(data.field),function (res) {
          $(".table_div").show();
          $(".operation").hide();
          loadData(1);
        },"PUT",false,function (res) {
          layer.msg("抱歉！您暂无权限");
        });
      }

      return false;
    });
  });
</script>